package com.note.simpleadmin.module.advert.controller;

import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.entity.TbBanner;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.advert.service.BannerService;
import com.note.simpleadmin.vo.Msg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/***
 *@ClaseName:BannerController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/29 0029 10:52
 */
@Controller
@RequestMapping(value = "advert/banner")
public class BannerController {
    private final static Logger logger = LoggerFactory.getLogger(BannerController.class);
    @Autowired
    private BannerService bannerService;
    @RequestMapping(value = "index")
    public String index() {
        return "advert/setting/banner";
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String type, String key, Integer page)
    {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            List<TbBanner> banners=bannerService.findAll();
            msg.setData(banners);
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "addBanner")
    public String addBanner()
    {
        return "advert/setting/addBanner";
    }
    @RequestMapping(value = "editBanner")
    public String editBanner(String id, ModelMap map)
    {
        TbBanner banner=bannerService.getOne(id);
        map.put("banner",banner);
        return "advert/setting/editBanner";
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(TbBanner banner)
    {
        Msg msg=new Msg();
        try {
            bannerService.save(banner);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("添加成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_update")
    @ResponseBody
    public Msg ajax_update(TbBanner banner)
    {
        Msg msg=new Msg();
        try {
            bannerService.update(banner);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("更新成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_del")
    @ResponseBody
    public Msg ajax_del(String id)
    {
        Msg msg=new Msg();
        try {
            bannerService.delete(id);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("删除成功");
        }catch (DataNotExistException e)
        {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
            msg.setMsg(e.getMessage());
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
}
